﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using AdAggregator.Attribute;
using AdAggregator.Util;

namespace ClothingAggregator.Utils
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString DropDownListForEnum<TModel, TEnum>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TEnum>> expression, Type enumType)
        {
            if (!typeof(TEnum).IsEnum) throw new ArgumentException("TEnum must be an enumeration type");
            var values = Enum.GetValues(enumType).Cast<Enum>();

            var items = values.Select(value => new SelectListItem
                {
                    Text = value.GetEnumName(),
                    Value = value.ToString()
                });
            return helper.DropDownListFor(expression, items);
        }

        public static MvcHtmlString DropDownListForSortableProperties<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, string>> expression, IEnumerable<PropertyInfo> props)
        {
            return helper.DropDownListFor(expression, props.Select((prop, i) => new SelectListItem
            {
                Text = ((Sortable)(prop.GetCustomAttribute(typeof(Sortable), false))).DisplayedName,
                Value = prop.Name
            }));
        }
    }
}